package com.bawei.network

import okhttp3.Interceptor
import okhttp3.MediaType
import okhttp3.RequestBody
import okhttp3.Response
import okio.Buffer
import org.json.JSONObject

/**
 * @author ZhangRuiPeng
 * @description:
 */
class SignInterceptor : Interceptor{
    override fun intercept(chain: Interceptor.Chain): Response {
        var request = chain.request()
        var sourcebody = request.body()
        var buff = Buffer()
        sourcebody!!.writeTo(buff)
        var sb = StringBuffer()
        var line = buff.readUtf8Line()
        while (line!=null){
            sb.append(line)
            line = buff.readUtf8Line()
        }
        buff.close()
        var parames = sb.toString()
        var job = JSONObject(parames)
        var keys  = job.keys()
        var signstr = String()
        while (keys.hasNext()){
            signstr += job.getString(keys.next())
        }
        var sign = SignUtils.createSign(signstr)
        job.put("sign",sign)
        var jsonstr = job.toString()
        var body = RequestBody.create(MediaType.parse("application/json"),jsonstr)
        var build = request.newBuilder()
        build.post(body)
        return chain.proceed(build.build())

    }





























}